/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
 *
 *           File:  dec8b10b.h
 *    Description:  8b10b decoder SystemC module
 *       
 *        Created:  13.02.2013 14:21:17
 *         Author:  Aleksey Golovchenko (lexgolovchenko@gmail.com)
 *   Organization:  St.Petersburg State Polytecnical Universisy
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#ifndef dec8b10b_h_
#define dec8b10b_h_

#include <systemc.h>

/*
 * Error value for decode table
 */
#define DEC8b10bERR "100000000"

/*
 * 8b10b decoder SystemC module
 */
SC_MODULE(dec8b10b)
{
    // ------- Input ports -------------------
    sc_in<bool> clk;
    sc_in<sc_uint<10> > enc_inp;

    // ------- Output ports ------------------
    sc_out<bool> err;
    sc_out<bool> ctrl;
    sc_out<sc_uint<8> > data_out;

    // ------- Internal signals/variables ----
    sc_uint<9> m_decode;

    // ------- Decode table ------------------
    static const char m_decode_table[1024][10];

    SC_CTOR(dec8b10b)
    {
        m_decode = 0;
        data_out.initialize(0);

        SC_METHOD(decode);
        sensitive << clk.pos();
    }

    // ------- Decoding method --------------- 
    void decode()
    {
        // get decoded value from table
        m_decode = sc_uint<9>( sc_bv<9>(m_decode_table[ enc_inp.read().to_uint()]) );
            
        // write output ports
        err.write((m_decode[8] && (m_decode.range(7,0) == 0)) ? 1 : 0);
        ctrl.write((m_decode[8] && (m_decode.range(7,0) != 0)) ? 1 : 0);
        data_out.write(m_decode.range(7,0));
    }    
};

#endif

